    package controllers;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import model.ButtonRequest;
import model.ButtonRequestModel;
import model.SubscriptionRequestModel;
import view.PendingRequestDetailsGUI;
import view.PendingRequestsGUI;
import client.CliMessage;


	/**
	 * This Class is a Pending Request Subscription Controller which extends AbstractController
	 * @author Tal
	 *
	 */

	public class PendingSubscriptionRequestsController extends AbstractController
	{
			
				private PendingRequestsGUI PendingRequestView;
				private ButtonRequestModel ButtonRequestModel;
			
				/**
				 * This is Pending Subscription Requests Controller constructor.
				 * @param PendingRequestView is a Pending Request Registration window GUI instance.
				 * @param ButtonRequestModel is a ButtonRequest Model instance.
				 */
				public PendingSubscriptionRequestsController(PendingRequestsGUI PendingRequestView, ButtonRequestModel ButtonRequestModel) 
				{
					super(PendingRequestView,ButtonRequestModel);
					this.PendingRequestView = PendingRequestView;
					this.ButtonRequestModel = ButtonRequestModel;		
				}
				
				/**
				 * This is setRequestsButtons method-this method add listeners to buttons in Pending Request window.
				 * 
				 */
				
				private void setRequestsButtons(ArrayList<ButtonRequest>  btnReq)
				{
					
					int nButtons = btnReq.size();
					
					
					///set a HashMap that contains all possible button listeners by their names
					ArrayList<ButtonListener> al = new ArrayList<ButtonListener>();
					ArrayList<ActionListener>  btnLis=new ArrayList<ActionListener>();
					
					///set  specific complaints buttons
					for(int i = 0; i < nButtons ; i++)
					{
						al.add(new ButtonListener(this,btnReq.get(i).getButtonReqID(),btnReq.get(i).getButtonUID()));
						btnLis.add(al.get(i));
						PendingRequestView.addButtonListener(btnLis.get(i),i);
					}	
				
				}
						
				/**
				 * Inner class that handles when Button Pressed by request Id, implements Action listener
				 * @author Tal
				 *
				 */
				
				class ButtonListener extends AbstractListener
				{
					private PendingSubscriptionRequestsController previusObserver;
					private String ReqID;
					private String Uid;
					
					public ButtonListener(PendingSubscriptionRequestsController previusObserver, String reqid,String uid)
					{
						this.ReqID=reqid;
						this.Uid=uid;
						this.previusObserver=previusObserver;
					}
					
					@Override
					public void actionPerformed(ActionEvent arg0) 
					{
						
						//create a new Pending Subcription Request Details controller
						PendingRequestDetailsGUI PendingRequestDetailsView = new PendingRequestDetailsGUI("Pending Subscrpition Request Details ");
						SubscriptionRequestModel subReqModel = new SubscriptionRequestModel();
						
						PendingSubscriptionRequestDetailsController pendSubDetController = new PendingSubscriptionRequestDetailsController(PendingRequestDetailsView,subReqModel);
						
						//add observer to new controller and delete the rest
						setCurrentObserver(pendSubDetController);						
						
						//add a listener to "back" button in  Pending Request Subscription Details window
						setBackListener(previusObserver,PendingRequestDetailsView);
						
						
						//remove buttons from arrayList
						ButtonRequestModel.RemoveButtonRequest();
						
						
						String searchQuery = "search"	+"SELECT *"
							                        	+" FROM subreq AS s, payment_method AS p, user u"
	                                                    +" WHERE s.reqid = " + ReqID + ""
	                                                    +" AND s.uid=u.uid AND p.pid=s.pid";
								
	                           
	                      sendToServer(searchQuery);
							
						
					}
				}


				@Override
				public void handleDBRequestSuccess()
				{
					PendingRequestView.displayWrnMessage("Pending Subscription Request Found:");
				}		

				@Override
				public void handleDBSearchResult(CliMessage dbResult) 
				{	
					//get a list of all Subscription Request
					ArrayList<HashMap<String, String>> srs = dbResult.getSrs();
					ArrayList<ButtonRequest>  btnReq= ButtonRequestModel.getButtonRequest();
					int rows=srs.size();
					int column=3;
					
					//create data table
					 Object[][] data=new Object[rows][column];
				   	
					 String[] columnNames={"Request Number", "First Name", "Last Name"};

					
					for(int i = 0 ; i <rows  ; i++)
					{
						Map<String, String> RequestList  = srs.get(i);	
						
	                    //add uid & reqId button to arraylist
						btnReq.add(new ButtonRequest(RequestList.get("REQID"),RequestList.get("UID")));
								
						//req num
						 data[i][0]=i+1;
						 //fname
						 data[i][1]=RequestList.get("FNAME");
						 //lname
						 data[i][2]=RequestList.get("LNAME");
						
						 System.out.println("user id:"+RequestList.get("UID")+" "+"user fname:"+RequestList.get("FNAME")+" "+"user lname:"+RequestList.get("LNAME"));
							 					
					}	
					//set new data table
					PendingRequestView.setTable(columnNames, data);
					
					//create n buttons
					int numberOfRequests = btnReq.size();
					
					PendingRequestView.setPendingRequestButtons(numberOfRequests);
					
					//Set list button  listener	
					setRequestsButtons(btnReq);
					 
					//display main window
					PendingRequestView.setVisible(true);
					PendingRequestView.setTableVisibilty(true);		
				
							
				}


				@Override
				public void handleNullSearchResult() 
				{
					//display main window
					PendingRequestView.setVisible(true);
					PendingRequestView.disposeTable();
					PendingRequestView.disposeExistingButtons();
					//msg
					PendingRequestView.displayWrnMessage("Pending Request Subscription Not Found!!!");
					
					
				}


}
